package com.wangdong.mijiqiqi;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.wangdong.mijiqiqi.threetype.ActionBarFragment;
import com.wangdong.wdbase.base.BaseDhqcActivity;


public class WebActivity extends BaseDhqcActivity {
	private ActionBarFragment actionBarFragment;
	private ProgressWebView mWebView;	
	private String mUrl;
	private String title;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.activity_webview);
		initData();
		initHeadView();
		initView();
		setData();
	}

	public void initData() {
		mUrl = getIntent().getStringExtra("url");
		title= getIntent().getStringExtra("title");
	}

	public void initHeadView() {
		try {
			actionBarFragment = (ActionBarFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_title);

			actionBarFragment.setLeftButton(R.drawable.back,
					new ActionBarFragment.IActionBarLeftOnClickListener() {
				@Override
				public void onClick(View view) {
					if (mWebView.canGoBack()) {
						mWebView.goBack();
					} else {
						finish();
					}
				}
			});
			actionBarFragment.setTitleName(title);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void initView() {
		try {
			mWebView = (ProgressWebView) findViewById(R.id.webView);
			setWebView(mWebView);
			initProgress(mWebView);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void setData() {
		try {
			mWebView.loadUrl(mUrl);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@SuppressWarnings("deprecation")
	@SuppressLint({ "SetJavaScriptEnabled", "InlinedApi" })
	private void setWebView(WebView webView) {
		try {
			webView.addJavascriptInterface(new JavaScriptObject(), "dhqc");  
			webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
			webView.getSettings().setJavaScriptEnabled(true);
			// webView.addJavascriptInterface(object, name)
			webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);// 允许js弹出窗口

			/*** 打开本地缓存提供JS调用 **/
			webView.getSettings().setDomStorageEnabled(true);
			// Set cache size to 8 mb by default. should be more than enough
			webView.getSettings().setAppCacheMaxSize(1024 * 1024 * 8);
			webView.setBackgroundColor(0xffffffff);
			// webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
			// This next one is crazy. It's the DEFAULT location for your app's
			// cache
			// But it didn't work for me without this line.
			// UPDATE: no hardcoded path. Thanks to Kevin Hawkins
			String appCachePath = getApplicationContext().getCacheDir().getAbsolutePath();
			webView.getSettings().setAppCachePath(appCachePath);
			webView.getSettings().setAllowFileAccess(true);
			webView.getSettings().setAppCacheEnabled(true);
			webView.getSettings().setUserAgentString("WDAndroid");
			webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
			webView.getSettings().setSupportZoom(false);
			webView.setScrollBarStyle(View.SCROLLBAR_POSITION_DEFAULT);
			// webView.setWebChromeClient(new MyWebChromeClient());
			webView.setWebViewClient(new MySystemWebViewClient());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	private class MySystemWebViewClient extends WebViewClient {
		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			// return super.shouldOverrideUrlLoading(view, url);
			view.loadUrl(url);
			return true;
		}

		@Override
		public void onPageStarted(WebView view, String url, Bitmap favicon) {
			super.onPageStarted(view, url, favicon);
			//progress();
		}

		@Override
		public void onPageFinished(WebView view, String url) {
			super.onPageFinished(view, url);
			success();
		}		
	}
	public class JavaScriptObject {  
	    @JavascriptInterface //sdk17版本以上加上注解
	    public void isLogin(boolean isLogin) {  
//	    	startActivityNotFinish(LoginActivity.class);
	    }  
	}  
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {

		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			if (mWebView.canGoBack()) {
				mWebView.goBack();
			} else {
				finish();
			}
			return true;
		}
		return super.onKeyDown(keyCode, event);

	}
	@Override
	public void update(Object obj, String tag) {
		super.update(obj, tag);
//		if(tag.equals(AppConstants.login)){
//			mWebView.reload();
//		}
	}
	@Override
	public void network() {
		super.network();
		mWebView.reload();
	}
}
